package com.zkjl.protect_business.utils;

import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.IWarningCallback;
import com.aspose.words.WarningType;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class PDFUtil {

    //实现匹配文件授权
    public static boolean matchLicense() {
        boolean result = false;
        InputStream is = PDFUtil.class.getClassLoader().getResourceAsStream("license.xml");
        License wordLicense = new License();
        try {
            wordLicense.setLicense(is);
            result = true;
        } catch (Exception e) {
            log.error("载入excel授权文件失败");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * word转pdf
     * @param sourceFile
     * @param targetFile
     */
    public static void word2PDF(File sourceFile, File targetFile) {
        if (!matchLicense()){
            throw new RuntimeException("匹配文件授权失败！");
        }


        FileOutputStream os = null;
        try {
            os = new FileOutputStream(targetFile);
            Document doc = new Document(sourceFile.getAbsolutePath());
            IWarningCallback callback = info -> {
                if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) {
                    log.info("获取字体: " + info.getDescription());
                }
            };
            doc.setWarningCallback(callback);
            // 以下代码指定字体库
            FontSettings fontSettings = new FontSettings();
            String[] fontsPath = new String[]{"/System/Library/Fonts/", "/Users/lgoodbook/Fonts/", "/usr/share/fonts/font/"};
            fontSettings.setFontsFolders(fontsPath, false);
            doc.setFontSettings(fontSettings);

            doc.save(os, com.aspose.words.SaveFormat.PDF);
        } catch (FileNotFoundException e) {
            log.error("输出到"+sourceFile.getAbsolutePath()+"错误:"+e);
        } catch (Exception e) {
            log.error("转换word出错:"+e);
        }finally {
            if(os!=null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error("关闭输出流出错:"+e);
                }
            }
        }
    }

    /**
     * excel转pdf
     * @param sourceFile
     * @param targetFile
     */
    public static void excel2PDF(File sourceFile, File targetFile) {
        if (!matchLicense()){
            throw new RuntimeException("匹配文件授权失败！");
        }
        FileOutputStream fileOs = null;
        Workbook wb = null;
        try {
            fileOs = new FileOutputStream(targetFile);
            //解决
            PdfSaveOptions saveOptions = new PdfSaveOptions();
            //重点，设置所有列放在一页里，会自动适应宽度
            saveOptions.setAllColumnsInOnePagePerSheet(true);

            wb = new Workbook(sourceFile.getAbsolutePath());
            wb.save(fileOs, saveOptions);
        } catch (FileNotFoundException e) {
            log.error("输出到"+sourceFile.getAbsolutePath()+"错误:"+e);
            e.getStackTrace();
        } catch (Exception e) {
            log.error("转换word出错:"+e);
        }finally {
            if(fileOs!=null) {
                try {
                    fileOs.close();
                } catch (IOException e) {
                    log.error("关闭输出流出错:"+e);
                }
            }
        }
    }





}
